﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace KCL.Code.Data
{
    public class MadeInDAO : BaseDAO
    {
        public List<MadeIn> GetAllMakes()
        {
            try
            {
                KCLDataContext ctx = new KCLDataContext();
                var lstMakes = from m in ctx.MadeIns
                               where m.isActive == true
                               orderby m.name
                               select m;
                return lstMakes.AsEnumerable().ToList();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public bool Insert(MadeIn m)
        {
            try
            {
                KCLDataContext ctx = new KCLDataContext();
                ctx.MadeIns.InsertOnSubmit(m);
                ctx.SubmitChanges();
                return true;
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                if (ex.Number == 2627)
                {
                    try
                    {
                        KCLDataContext ctx = new KCLDataContext();
                        var makeToRestore = (from mk in ctx.MadeIns
                                             where mk.name.ToUpper() == m.name.ToUpper()
                                             && mk.isActive == false
                                             select mk).FirstOrDefault();
                        makeToRestore.name = m.name;
                        makeToRestore.isActive = true;
                        ctx.SubmitChanges();
                        return true;
                    }
                    catch (Exception)
                    {
                        return false;
                    }
                }
                return false;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Update(MadeIn m)
        {
            try
            {
                KCLDataContext ctx = new KCLDataContext();
                var makeToUpdate = (from mk in ctx.MadeIns
                                    where mk.id == m.id
                                    select mk).FirstOrDefault();
                makeToUpdate.name = m.name.Trim();
                makeToUpdate.isActive = m.isActive;
                if (makeToUpdate.name.Length == 0)
                    return false;
                ctx.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Delete(MadeIn m)
        {
            try
            {
                KCLDataContext ctx = new KCLDataContext();
                var makeToDelete = (from mk in ctx.MadeIns
                                    where mk.id == m.id
                                    select mk).FirstOrDefault();
                makeToDelete.isActive = false;
                ctx.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}